# 竟然这么快……
class WordDictionary:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.lendict = {}

    def addWord(self, word: str) -> None:
        """
        Adds a word into the data structure.
        """
        wlen = len(word)
        if wlen not in self.lendict:
            self.lendict[wlen] = set()
        self.lendict[wlen].add(word)

    def search(self, word: str) -> bool:
        """
        Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
        """
        wlen = len(word)
        if wlen not in self.lendict:
            return False

        for it in self.lendict[wlen]:
            if self.match(it, word):
                return True
        return False

    def match(self, word: str, patt: str):
        for index, letter in enumerate(patt):
            if letter == '.':
                continue
            if letter != word[index]:
                return False
        return True


# Your WordDictionary object will be instantiated and called as such:
obj = WordDictionary()
obj.addWord("at")
obj.addWord("and")
obj.addWord("an")
obj.addWord("add")
print(obj.search("a")) # false
print(obj.search(".at")) # false

obj.addWord("bat")
print(obj.search(".at"))  # true
print(obj.search("an."))  # true
print(obj.search("a.d.")) # false
print(obj.search("b."))   # false
print(obj.search("a.d"))  # true
print(obj.search("."))    # false